package main

import (
	"fmt"
	"io/fs"
	"io/ioutil"
	"os"
	"path"
	"runtime"
	"sync"
)

var removeList = []string{
	//"C:\\Users\\admin\\AppData\\Local\\Temp",
	//"C:\\Users\\admin\\AppData\\Local\\go-build",
	//"C:\\Users\\admin\\AppData\\Local\\Yarn\\Cache\\v6",
	//"F:\\admin\\.m2\\repository",
	"F:\\cacheData\\Yarn\\Cache\\v6",
}
var (
	FilesLen int
	R        sync.Mutex
	Isend    bool = false
	jishuqi  int  = 0
)

func main() {
	num := runtime.NumCPU()
	runtime.GOMAXPROCS(num - 2)
	fmt.Println("Cpu 核心数量：", num)
	fmt.Printf("Info: Start\n")
	FilesLen = getlens()
	fmt.Printf("%v\n", FilesLen)
	if FilesLen > 0 {
		ch := make(chan int, 8)
		todel(ch)
		End(ch, Isend)
	}
	fmt.Printf("Info: End\n")

}
func End(ch chan int, isend bool) {
	for {
		select {
		case _ = <-ch:
			//fmt.Printf("\n第%v个结束\n", j)
			if isend {
				break
			}
		}
	}
}
func getlens() int {
	var length int
	for _, p := range removeList {
		dir, _ := ioutil.ReadDir(p)
		length += len(dir)
	}
	return length
}
func todel(ch chan<- int) {
	for i, p := range removeList {
		//fmt.Printf("\n第%v个\n", i)
		if dir, _ := ioutil.ReadDir(p); len(dir) > 0 {
			R.Lock()
			go delfile(p, ch, i)
			if jishuqi >= FilesLen {
				Isend = true
				break
			}
			R.Unlock()
		}

	}
}
func delfile(filepath string, ch chan<- int, i int) {
	dir, _ := ioutil.ReadDir(filepath)
	for _, d := range dir {
		fmt.Print(d.Name() + "\n")
		go dodel(filepath, d, ch, i)

	}
}
func dodel(filepath string, d fs.FileInfo, ch chan<- int, i int) {
	os.RemoveAll(path.Join([]string{filepath, d.Name()}...))
	jishuqi += 1
	fmt.Println("计数器：", jishuqi)
	ch <- i
}
